package com.wfh.rpc.loadbalancer;

import com.wfh.rpc.model.ServiceModelInfo;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @Title: RandomLoadBalancer
 * @Author wangfenghuan
 * @Package com.wfh.rpc.loadbalancer
 * @Date 2025/10/16 21:02
 * @description: 随机负载均衡器
 */
public class RandomLoadBalancer implements LoadBalancer{

    private final Random random = new Random();

    @Override
    public ServiceModelInfo select(Map<String, Object> requestParams, List<ServiceModelInfo> serviceModelInfoList) {
        int size = serviceModelInfoList.size();
        if (size == 0){
            return null;
        }
        // 只有一个服务，不需要随机
        if (size == 1){
            return serviceModelInfoList.get(0);
        }
        return serviceModelInfoList.get(random.nextInt(size));
    }
}
